﻿using EMoney.IStock.Client.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.Business
{
    /// <summary>
    /// 发送短信、邮件逻辑处理类
    /// </summary>
    public class SendBll
    {
        private const string SMS_MEMCACHED_KEY = "EMoney.IStock.Client.Business.SendBll.SMS";
        private const string EMAIL_MEMCACHED_KEY = "EMoney.IStock.Client.Business.SendBll.EMAIL";
        private const string CODE_MEMCACHED_KEY = "EMoney.IStock.Client.Business.SendBll.Code";

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="sendKey"></param>
        /// <param name="mobile"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool SendSms(string sendKey, string mobile, string content)
        {
            if (string.IsNullOrEmpty(sendKey) || sendKey == "0")
            {
                return false;
            }

            bool flag = false;
            string key = string.Concat(SMS_MEMCACHED_KEY, ".", sendKey);

            string result = CacheManager.RedisCache.Get<string>(key);

            if (string.IsNullOrEmpty(result))
            {
                if (ConfigHelper.IsOnline == "0")
                {
                    flag = true;
                }
                else
                {
                    if (SendHelper.SendSms(mobile, content))
                    {
                        flag = true;
                        CacheManager.RedisCache.Set<string>(key, "1", 1);    //缓存1分钟
                    }
                }
            }
            else
            {
                flag = true;
            }

            return flag;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="sendKey"></param>
        /// <param name="email"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static bool SendEmail(string sendKey, string email, string subject, string body)
        {
            if (string.IsNullOrEmpty(sendKey) || sendKey == "0")
            {
                return false;
            }

            bool flag = false;
            string key = string.Concat(EMAIL_MEMCACHED_KEY, ".", sendKey);

            string result = CacheManager.RedisCache.Get<string>(key);

            if (string.IsNullOrEmpty(result))
            {
                if (SendHelper.SendEmail(email,subject,body))
                {
                    flag = true;
                    CacheManager.RedisCache.Set<string>(key, "1", 1);    //缓存1分钟
                }
            }
            else
            {
                flag = true;
            }

            return flag;
        }

        /// <summary>
        /// 缓存发送的验证码
        /// </summary>
        /// <param name="account"></param>
        /// <param name="code"></param>
        public static void CacheCode(string account, string code)
        {
            string key = string.Concat(CODE_MEMCACHED_KEY, ".", account);

            string val = CacheManager.RedisCache.Get<string>(key);

            if (string.IsNullOrEmpty(val))
            {
                CacheManager.RedisCache.Set<string>(key, code, 30);
            }
        }

        /// <summary>
        /// 获取发送的验证码
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public static string GetCode(string account)
        {
            string key = string.Concat(CODE_MEMCACHED_KEY, ".", account);

            return CacheManager.RedisCache.Get<string>(key);
        }
    }
}
